#include"PF_List.h"

// Define the static member variable
PF_List* PF_List::originalPFList = nullptr;

int  PF_List::setAvailable(int id,bool available)
{
        int role,profession;
        int ret=ID_To_PF_Convert(id,role,profession);
        if(ret != 0) return ret; // Conversion failed



        items[role][profession].setAvailable(available);
        return 0;
};

int PF_List::setItem(int id, const PF_Item& item) {
    int role, profession;
    int ret = ID_To_PF_Convert(id, role, profession);

    if(ret!=0)return ret;

    items[role][profession] = item; // Set the item at the specified position
    return 0;
}

void PF_List::setItems(vector<vector<PF_Item>>& items) {
    this->items = items; // Set the items vector to the provided items
}



void PF_List::getOriginalPFListCopy(PF_List& pfList) {
    if(originalPFList == nullptr) {
        // If originalPFList is not initialized, we cannot copy it
        originalPFList = new PF_List(); 
        for(int i = 0; i < MAX_ROLE_COUNT; ++i) {
            vector<PF_Item> roleItems;
            for(int j = 0; j < MAX_PROFESSION_COUNT; ++j) {
                int id;
                PF_To_ID_Convert(i, j, id);
                roleItems.emplace_back(id, true); // Initialize with available items
            }

            originalPFList->items.push_back(roleItems);
        }
    }

    pfList = *originalPFList; // Copy the original PF_List to the provided pfList
}

int PF_List::getItem(int id,PF_Item& item)
{
        int role, profession;
        int ret = ID_To_PF_Convert(id, role, profession);
        if (ret != 0) return ret; // Conversion failed

        if (role < 0 || role >= items.size() || profession < 0 || profession >= items[role].size()) {
            return -1; // Invalid role or profession
            perror("Invalid role or profession");
        }

        item = items[role][profession]; // Get the item at the specified position
        return 0; // Success
}

int PF_List::getItem(int role,int profession, PF_Item& item){
    if(role < 0 || role >= items.size() || profession < 0 || profession >= items[role].size()) {
        return -1; // Invalid role or profession
        perror("Invalid role or profession");
    }

    item = items[role][profession]; // Get the item at the specified position
    return 0; // Success
}


    
int PF_List::BanProfession(int teamID, int PF_ID) {
    PF_Item item;
    if(getItem(PF_ID,item)!=0||!item.getisAvailable()){
        return 1;
    }
    item.setTeamID(teamID) ;
    item.setAvailable(false);
    item.setItemState(PF_Item::ItemState::BANNED);
    return 0;
}

int PF_List::PickProfession(int teamID, int PF_ID) {
    PF_Item item;
    if(getItem(PF_ID,item)!=0||!item.getisAvailable()){
        return 1;
    }
    item.setTeamID(teamID);
    item.setAvailable(false);
    item.setItemState(PF_Item::ItemState::PICKED);
    return 0;
}

int PF_List::AssignProfessions(int teamID, const vector<int>& PF_IDs) {

    int ret = 0;
    for(int i=0;i<PF_IDs.size();i++){
        int r=AssignProfession(teamID,i,PF_IDs[i]);
        if(r!=0)ret=r;
    }
    return ret;
}

int PF_List::AssignProfession(int teamID,int assignID, int PF_ID){
    PF_Item item;
    if(getItem(PF_ID,item)!=0||!item.getisAvailable()){
        return 1;
    }


    item.Picked(teamID,assignID);
    return 0;
}